<template>
  <div id="reportIndexItem">
    <h2 class="title"><span>{{$route.query.itemID}}</span></h2>
    <div class="vsContent">
      <div class="vsThead">
        <table class="table">
          <thead>
            <tr>
              <th class="text-center" width="25%">时间</th>
              <th class="text-center" width="35%">项目名称</th>
              <th class="text-center" width="10%">结果</th>
              <th class="text-center" width="10%">单位</th>
              <th class="text-center" width="20%">参考值</th>
            </tr>
          </thead>
        </table>
      </div>
      <div class="vsTbody" v-if="reportIndexData.length">
        <table class="table table-striped">
          <tbody>
            <tr v-for="item in reportIndexData">
              <td class="text-center" width="25%">{{item.time_check | filterDateT}}</td>
              <td class="text-center" width="35%">{{item.REPORT_ITEM_NAME}}</td>
              <td :class="['text-center',{'red': item.ABNORMAL_INDICATOR != ' '}]" width="10%">{{item.RESULT}}&nbsp;{{item.ABNORMAL_INDICATOR}}</td>
              <td class="text-center" width="10%">{{item.UNITS}}</td>
              <td class="text-center" width="20%">{{item.REFERENCE_RESULT}}</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
    <lineEcharts v-if="height!=''&& itemData.length != 0" :style="{'height': height}" class="line" :height="height" :reference="reference" :name="$route.query.itemID+'趋势图'" :items="itemData"></lineEcharts>
    <loading v-if="reportIndexData.length==0 && isShow ||  JSON.stringify(itemData)=='{}' && isShow "></loading>
    <noSearch v-if="reportIndexData.length==0 && !isShow"></noSearch>
  </div>
</template>
<script>
  import { apiInspectionItem } from '@/api/api'
  import lineEcharts from '@/components/echarts/lineEcharts'
  import loading from '@/components/loading/loading'
  import noSearch from '@/components/search/noSearch' //导入没有搜索结果组件
  export default {
    data() {
      return {
        reportIndexData: [],
        itemData: [],
        height: '',
        isShow: true,
        reference:[]
      }
    },
    activated() {
      this.reportIndexData = [];
      this.itemData = [];
      this.getInspectionItem();
    },
    methods: {
      getInspectionItem() {
        apiInspectionItem({
          patientID: this.$store.getters.ID,
          itemName: this.$route.query.itemID
        })
          .then(res => {
            this.itemData = [];
            this.reference = [];
            if (JSON.parse(res).Status == 1) {
              this.isShow = true;
              this.reportIndexData = JSON.parse(res).Data;
              for (var item of JSON.parse(res).Data) {
                this.itemData.push({ date: this.filterDate(item.time_check), value: item.RESULT })
              }
              //console.log(JSON.parse(res).Data)
              let val = JSON.parse(res).Data[0].REFERENCE_RESULT
              if (typeof (this.filterReference(val)) == 'string' && this.filterReference(val) != '') {
                this.reference = [{ 'yAxis' : 0}]
                this.reference.push({ 'yAxis': this.filterReference(val) })
              } else {
                for (let ind in this.filterReference(val)) {
                  this.reference.push({ 'yAxis': this.filterReference(val)[ind] })
                }
              }
              //console.log(this.reportIndexData)
              this.$nextTick(function () {
                this.getHeight();
              })
              //console.log(this.reportIndexData)
            } else if (JSON.parse(res).Status == -1) {
              this.isShow = false;
              this.reportIndexData = [];
            }
          })
          .catch(err => {
            this.$toast.center('当前请求接口有问题，请联系管理员！！')
          });
      },
      getHeight() {
        this.height = $('#reportIndexItem').height() - $('.vsTbody').height() - 75 + 'px'
      },
      filterDate(time) {
        if (!time) {
          return '';
        }
        else {
          time = time.replace(/\//g, '-');
          time = time.replace(/T/g, ' ');
          return time.substring(5, 16);
        }
      },
      filterReference(str) {
        if (!str) {
          return ''
        } else {
          if (str.indexOf('<') != -1) {
            return str.replace('<', '');
          } else if (str.indexOf('-') != -1) {
            return str.split('-');
          }
        }
      }
    },
    components: {
      lineEcharts,
      loading,
      noSearch
    }
  }
</script>
<style scoped>
  #reportIndexItem {
    height:calc(100vh - 145px);
    padding: 20px 0 0;
  }

  .title {
    font-size: 18px;
    padding-left: 40px;
  }

    .title span:before {
      content: '';
      display: inline-block;
      width: 3px;
      height: 20px;
      border-radius: 8px;
      margin-right: 8px;
      background: #11a7f3;
      vertical-align: text-top;
    }

  .table > thead > tr > th {
    border-bottom: none;
  }



  .vsThead, .vsTbody {
    width: 100%;
    /*border:1px solid #f4f4f4;*/
    border-top: none;
  }

  .vsContent {
    margin-top: 10px;
    padding:0 40px;
  }

  .table {
    margin-bottom: 0;
  }

  .vsThead thead tr {
    height: 42px;
    line-height: 42px;
    background-color: #11a7f3;
    color: #fff;
  }

  .vsThead > thead > tr > th {
    border-bottom: none;
  }

  .vsTbody {
    max-height: calc(100vh - 600px);
    overflow-y: auto;
    border: 1px solid #ddd;
  }

    .vsTbody tbody tr td {
      padding: 10px 0;
      vertical-align: middle;
      border-top: 1px solid #ddd;
      color: #333;
      border-right: 1px solid #ddd;
    }

      .vsTbody tbody tr td:last-child {
        border-right: none;
      }

    .vsTbody tbody > tr:first-child > td {
      border-top: none;
    }
    .vsTbody  tbody tr td.red {
      color: red;
    }
  .line {
    margin-top: 20px;
  }
  @media screen and (max-width: 576px){
    .vsContent{
      padding: 0 10px;
    }
  }
</style>
